package com.coollang.smartbra.utils;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.LocationManager;
import android.net.Uri;
import android.util.Log;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2016/10/19 0019.
 */

public class GPSUtils {

    public static String TAG = "GPSUtils";
    public static List<GpsSatellite> numSatelliteList = new ArrayList<GpsSatellite>();
    public static int mSatelliteNum;
    //state 0 信号低 1信号中 2信号强
    public static int GetGPSStatus(int event, GpsStatus status) {
        // Log.d(TAG, "enter the updateGpsStatus()");
        if (status == null) {

        } else if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
            //获取最大的卫星数（这个只是一个预设值）
            int maxSatellites = status.getMaxSatellites();
            Iterator<GpsSatellite> it = status.getSatellites().iterator();
            numSatelliteList.clear();
            //记录实际的卫星数目
            int count = 0;
            while (it.hasNext() && count <= maxSatellites) {
                //保存卫星的数据到一个队列，用于刷新界面
                GpsSatellite s = it.next();
                numSatelliteList.add(s);
                count++;
                Log.d(TAG, "updateGpsStatus----count=" + count);
            }
            mSatelliteNum = numSatelliteList.size();
            int countP = count * 100 / maxSatellites;
            if (countP <= 40) {
                return 0;
            } else if (countP < 70 && countP > 40) {
                return 1;
            } else if (countP <= 100 && countP >= 70) {
                return 2;
            }
        } else if (event == GpsStatus.GPS_EVENT_STARTED) {
            //定位启动
        } else if (event == GpsStatus.GPS_EVENT_STOPPED) {
            //定位结束
        }
        return 0;
    }

    /**
     * 判断GPS是否开启，GPS或者AGPS开启一个就认为是开启的
     * @param context
     * @return true 表示开启
     */
    public static final boolean isOPen(final Context context) {
        LocationManager locationManager
                = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        // 通过GPS卫星定位，定位级别可以精确到街（通过24颗卫星定位，在室外和空旷的地方定位准确、速度快）
        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        // 通过WLAN或移动网络(3G/2G)确定的位置（也称作AGPS，辅助GPS定位。主要用于在室内或遮盖物（建筑群或茂密的深林等）密集的地方定位）
        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
//        || network
        if (gps) {
            return true;
        }
        return false;
    }

    /**
     * 强制帮用户打开GPS
     * @param context
     */
    public static final void openGPS(Context context) {
        Intent GPSIntent = new Intent();
        GPSIntent.setClassName("com.android.settings",
                "com.android.settings.widget.SettingsAppWidgetProvider");
        GPSIntent.addCategory("android.intent.category.ALTERNATIVE");
        GPSIntent.setData(Uri.parse("custom:3"));
        try {
            PendingIntent.getBroadcast(context, 0, GPSIntent, 0).send();
        } catch (PendingIntent.CanceledException e) {
            e.printStackTrace();
        }
    }
}
